package thread;

public class SyncDemo {
    public static void main(String[] args) {
        Table table=new Table();

        Thread t1=new Thread("Amy"){
            @Override
            public void run() {
                while (true) {
                    int bean=table.getBean();
                    Thread.yield();
                    System.out.println(getName()+": "+bean);
                }
            }
        };
        Thread t2=new Thread("Bob"){
            @Override
            public void run() {
                while (true) {
                    int bean=table.getBean();
                    Thread.yield();
                    System.out.println(getName()+": "+bean);
                }
            }
        };
        t1.start();
        t2.start();
    }
}
class Table  {
    int bean=20;

    public synchronized int getBean() {
        if(bean==0){
            throw new RuntimeException("There's no bean on the table");
        }
        Thread.yield();
       return bean--;
    }
}